package cn.fch.gotrip.service.impl;

import cn.fch.gotrip.dao.TravelDao;
import cn.fch.gotrip.entity.Travel;
import cn.fch.gotrip.entity.TravelActivity;
import cn.fch.gotrip.entity.TravelTheme;
import cn.fch.gotrip.entity.TravelWay;
import cn.fch.gotrip.service.TravelService;
import cn.fch.gotrip.util.UploadUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @program: gotrip
 * @description:
 * @CreatedBy: fch
 * @create: 2023-01-16 00:04
 **/
@Service
public class TravelServiceImpl implements TravelService {

    @Autowired
    TravelDao travelDao;

    @Autowired
    UploadUtil uploadUtil;

    private final static String TRAVEL_CHILD_PATH = "static\\img\\travel-img";

    @Override
    public boolean addTravel(Travel travel) {
        boolean res = false;
        if (travelDao.addTravel(travel) > 0){
            travel.setImg(travel.getId() + ".png");
            travelDao.updateTravel(travel);
            res = true;
        }
        return res;
    }

    @Override
    public boolean deleteTravel(Integer id) {
        return travelDao.deleteTravel(id) > 0;
    }

    @Override
    public boolean updateTravel(Travel travel) {
        return travelDao.updateTravel(travel) > 0;
    }

    @Override
    public String updateImg(String id, MultipartFile file, HttpServletRequest request) throws IOException {
        if (id.contains("?")){
            id = id.split("[?]")[0];
        }
        return uploadUtil.uploadImg(TRAVEL_CHILD_PATH, id, file, request);
    }

    @Override
    public PageInfo<Travel> queryAllTravel(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Travel> list = travelDao.queryAllTravels();
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Travel> queryAllTravelByThemeId(Integer themeId, Integer pageNum, Integer pageSize) {
        List<Travel> list;
        if (0 == themeId) {
            PageHelper.startPage(pageNum, pageSize);
            list = travelDao.queryAllTravels();
        } else {
            PageHelper.startPage(pageNum, pageSize);
            list = travelDao.queryAllTravelByThemeId(themeId);
        }
        return new PageInfo<>(list);
    }

    @Override
    public List<TravelTheme> queryAllTheme() {
        return travelDao.queryAllTheme();
    }

    @Override
    public List<TravelActivity> queryAllActivity() {
        return travelDao.queryAllActivity();
    }

    @Override
    public List<TravelWay> queryAllWay() {
        return travelDao.queryAllWay();
    }
}
